package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domain.Category;
import com.itheima.domain.Setmeal;
import com.itheima.domain.SetmealDish;
import com.itheima.dto.SetmealDto;
import com.itheima.service.CategoryService;
import com.itheima.service.SetmealDishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;



    /**
     * 添加套餐
     */
    @PostMapping
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @Transactional
    public R<Page<SetmealDto>> getpage(int page, int pageSize, String name) {
        //添加查询条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByAsc(Setmeal::getCode).orderByDesc(Setmeal::getUpdateTime);
        //设置分类
        Page<Setmeal> ipage = new Page<>(page, pageSize);
        Page<SetmealDto> dtopage = new Page<>();
        //查询
        setmealService.page(ipage, lqw);
        //复制数据
        BeanUtils.copyProperties(ipage, dtopage, "records");
        List<Setmeal> records = ipage.getRecords();
        List list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            //查询套餐名称
            Long categoryId = item.getCategoryId();
            Category cate = categoryService.getById(categoryId);
            //赋值套餐名
            setmealDto.setCategoryName(cate.getName());
            return setmealDto;
        }).collect(Collectors.toList());
        dtopage.setRecords(list);
        return R.success(dtopage);
    }

    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping
    @Transactional
    @CacheEvict(value = "setmeal",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return  R.success("删除成功");
    }

    /**
     * 停售
     * @param ids
     * @return
     */
    @PostMapping("/status/0")
    public R<String> ban(String ids){
        String[] split = ids.split(",");
        for (String strId : split){
            long id = Long.parseLong(strId);
            LambdaUpdateWrapper<Setmeal> lqw =new LambdaUpdateWrapper<>();
            lqw.set(Setmeal::getStatus,0).eq(Setmeal::getId,id);
            setmealService.update(lqw);
        }
        return R.success("停售成功");
    }

    /**
     *   起售
     * @param ids
     * @return
     */
    @PostMapping("/status/1")
    public R<String> start(String ids){
        String[] split = ids.split(",");
        for (String strId : split){
            long id = Long.parseLong(strId);
            LambdaUpdateWrapper<Setmeal> lqw =new LambdaUpdateWrapper<>();
            lqw.set(Setmeal::getStatus,1).eq(Setmeal::getId,id);
            setmealService.update(lqw);
        }
        return R.success("起售成功");
    }

    /**
     * 套餐查询
     * @param categoryId
     * @param status
     * @return
     */
    @GetMapping("list")
    @Cacheable(value = "setmeal" , key ="#categoryId+'_'+#status" ,unless = "#result.data ==null")
    public R<List<Setmeal>> list(Long categoryId , int status){
        LambdaQueryWrapper<Setmeal> lqw =new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,status);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }
}
